﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Automation;
using Microsoft.Win32;
using System.IO;
using System.ComponentModel;
using System.Media;
using Tedds_Tool;
using CommonInterfaces.Models;

namespace GUI.Custom_Classes
{
    /// <summary>
    /// Interaction logic for CreateCons.xaml
    /// </summary>
    public partial class CreateCons : Window
    {
        private IScriptFinder scriptFinder;

        public CreateCons(IElementState m, IScriptFinder scriptFinder)
        {

            this.scriptFinder = scriptFinder;
            InitializeComponent();
            createButton.IsDefault = true;
            cancelButton.IsCancel = true;
        }

        #region Button Events

        private void cancel_Button(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void createAction(object sender, RoutedEventArgs e)
        {
            if (String.IsNullOrEmpty(nameBox.Text))
            {
                SystemSounds.Beep.Play();
                MessageBox.Show("You must specify a name for the action.", "Warning", MessageBoxButton.OK, MessageBoxImage.Warning, MessageBoxResult.OK);
            }
            else if (Fatality.SelectedIndex == 0)
            {
                SystemSounds.Beep.Play();
                MessageBox.Show("Specify target element(s), all or specific.", "Warning", MessageBoxButton.OK, MessageBoxImage.Warning, MessageBoxResult.OK);
            }
            else
            {
                String scriptName = nameBox.Text;
                BackgroundWorker bgw = new BackgroundWorker();
                bgw.DoWork += new DoWorkEventHandler(delegate
                {
                    using (TextWriter sw = scriptFinder.CreateConsequence(scriptName))
                    {
                        string s;

                        Fatality.Dispatcher.Invoke
                        (
                            (Action)(() =>
                            {
                                s = Fatality.Text;
                                if (s == "Fatal")
                                    s = "1 | ";
                                else
                                    s = "0 | ";

                                s += ConsequenceText.Text;
                                sw.WriteLine(s);
                            }
                        ));

                    }
                });

                bgw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(delegate
                {
                    MessageBox.Show("Consequence successfully saved.", "Save Complete", MessageBoxButton.OK, MessageBoxImage.Information, MessageBoxResult.OK);
                });

                bgw.RunWorkerAsync();
                Close();
            }
        }

        #endregion

    }
}
